import qlib
from multiprocessing import freeze_support
from qlib.data import D
from datetime import datetime
from qlib.data.filter import NameDFilter

class QlibMgr:
    def __init__(self):
        qlib.init(provider_uri='~/.qlib/qlib_data/fund_data')

    def load_data(self, codes, fields, start_time='2010-01-01',end_time=datetime.now().strftime('%Y-%m-%d')):
        df = D.features(codes, fields, start_time=start_time,end_time=end_time,
                        freq='day')
        df.reset_index(level=0, inplace=True)
        df.sort_index(ascending=True, inplace=True)
        return df


if __name__ == '__main__':
    freeze_support()
    df = QlibMgr().load_data(['512690.SH','159715.SZ'],['$close/Ref($close,1)-1'])
    print(df[df['instrument']=='512690.SH'])